/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.framework.implementation;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Window;
import android.view.WindowManager;
import com.zhangman.framework.Audio;
import com.zhangman.framework.FileIO;
import com.zhangman.framework.Game;
import com.zhangman.framework.Graphics;
import com.zhangman.framework.Input;
import com.zhangman.framework.Screen;

/**
 * Created by zhangman on 2016/11/1 10:35.
 * Email: zhangman523@126.com
 */
public abstract class AndroidGame extends Activity implements Game {
  AndroidFastRenderView mRenderView;
  Graphics mGraphics;
  Audio mAudio;
  Input mInput;
  FileIO mFileIO;
  Screen mScreen;
  PowerManager.WakeLock mWakeLock;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    boolean isPortrait =
        getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 800 : 1280;
    int frameBufferHeight = isPortrait ? 1280 : 800;
    Bitmap frameBuffer =
        Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Bitmap.Config.RGB_565);
    float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight();

    mRenderView = new AndroidFastRenderView(this, frameBuffer);
    mGraphics = new AndroidGraphics(getAssets(), frameBuffer);
    mFileIO = new AndroidFileIO(this);
    mAudio = new AndroidAudio(this);
    mInput = new AndroidInput(this, mRenderView, scaleX, scaleY);
    mScreen = getInitScreen();
    setContentView(mRenderView);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
  }

  @Override protected void onResume() {
    super.onResume();
    mWakeLock.acquire();
    mScreen.resume();
    mRenderView.resume();
  }

  @Override protected void onPause() {
    super.onPause();
    mWakeLock.release();
    mRenderView.pause();
    mScreen.pause();
    if (isFinishing()) {
      mScreen.dispose();
    }
  }

  @Override public Input getInput() {
    return mInput;
  }

  @Override public FileIO getFileIO() {
    return mFileIO;
  }

  @Override public Graphics getGraphics() {
    return mGraphics;
  }

  @Override public Audio getAudio() {
    return mAudio;
  }

  @Override public void setScreen(Screen screen) {
    if (screen == null) {
      throw new IllegalArgumentException("Screen must not be null");
    }
    this.mScreen.pause();
    this.mScreen.dispose();
    screen.resume();
    screen.update(0);
    this.mScreen = screen;
  }

  @Override public Screen getCurrentScreen() {
    return mScreen;
  }
}
